package com.hectopascal.goalance.service;

import com.hectopascal.goalance.pojo.Notification;
import com.hectopascal.goalance.pojo.dto.NotificationBack;
import com.hectopascal.goalance.pojo.dto.UserBack;
import com.hectopascal.goalance.repository.NotificationRepository;
import com.hectopascal.goalance.repository.UserRepository;
import com.hectopascal.goalance.service.Ipml.INotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NotificationService implements INotificationService {
    @Autowired
    NotificationRepository notificationRepository;
    @Autowired
    UserRepository userRepository;

    @Override
    public List<NotificationBack> getNotification(Integer id) {
        return notificationRepository.getAllNotification(id).get();
    }

    @Override
    public List<NotificationBack> getSendNotification(Integer userId) {
        return notificationRepository.getSendNotification(userId).get();
    }

    @Override
    public int isRead(Integer id) {
        return notificationRepository.updateIsRead(id);
    }

    @Override
    public int sendNotification(Notification notification,Integer target){
        List<UserBack> recipients;
        if (target==0) {
            // 获取所有员工
            System.out.println(notification.getAuthorId());
            Integer departmentId = userRepository.getDepartmentId(notification.getAuthorId());
            System.out.println(departmentId);
            recipients = userRepository.findByRoleAndDep("employee",departmentId);
        } else if(target==1){
            //获取所有员工和主管
            recipients = userRepository.findByRole("employee");
            recipients.addAll(userRepository.findByRole("manager"));
        } else {
            // 发送给指定用户
            recipients = Collections.singletonList(userRepository.findUserById(notification.getUserId()).get());
        }

        // 批量插入通知
        List<Notification> notifications = recipients.stream()
                .map(user -> {
                    Notification notification1 = new Notification();
                    notification1.setUserId(user.getId());
                    notification1.setAuthorId(notification.getAuthorId());
                    notification1.setType(notification.getType());
                    notification1.setContent(notification.getContent());
                    notification1.setIsRead(0);
                    notification1.setCreatedAt(new Date());
                    return notification1;
                })
                .collect(Collectors.toList());
        System.out.println(notifications);
        notificationRepository.saveAll(notifications);
        return notifications.size();
    }

    @Override
    public int deleteNotification(Integer id) {
        return notificationRepository.deleteNotification(id);
    }

    @Override
    public List<NotificationBack> getUnreadNotification(Integer userId){
        return notificationRepository.getUnreadNotification(userId).get();
    }
}
